<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

	<head>
		<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
		<script type="text/javascript" src="../../js/boot.js"></script>
		<title>数据库连接管理</title>
	</head>

	<body>
		<div class="mini-splitter" style="width: 100%; height: 100%;">
			<div size="240" showCollapseButton="true">
				<div class="mini-fit">
					<div class="mini-toolbar" style="padding: 2px; border-top: 0; border-left: 0; border-right: 0;">
						<a class="mini-button" iconCls="icon-add" onclick="openAddWin()">添加</a>
						<a class="mini-button" iconCls="icon-remove" onclick="doDelete()">删除</a>
						<a class="mini-button" iconCls="icon-save" onclick="doSave()">保存</a>
					</div>
					<div id="tableList" class="mini-listbox" style="width:99%;height:94%;" textField="databaseName" valueField="databaseName">
					</div>
				</div>
			</div>

			<div showCollapseButton="true">
				<div class="mini-fit">
					<div id="fieldGrid" class="mini-datagrid" style="width:100%;height:100%;" allowCellEdit="true" allowCellSelect="true" editNextOnEnterKey="true" editNextRowCell="true">
						<div property="columns">
							<div field="index" type="indexcolumn" width="16"></div>
							<div field="key" headerAlign="center" width="100">属性名</div>
							<div field="val" name="val" headerAlign="center" width="200">属性值
								<input property="editor" class="mini-textbox" style="width:100%;" />
							</div>
							<div field="des" headerAlign="center" width="200">说明</div>
						</div>
					</div>
				</div>
			</div>
		</div>

	</body>

	<script type="text/javascript">
		miniInit();

		var tableList = mini.get("tableList"); // 表定义列表
		tableList.set({
			url: http.getUrl("/admin/database/list.do")
		});

		var fieldGrid = mini.get("fieldGrid");
		// 表名的单击事件，切换显示表所包含的所有字段信息
		tableList.on("itemclick", function(e) {
			var item = e.item;
			setFieldGridData(item);
		});

		/*******************************************
		 * 打开新增窗体
		 *******************************************/
		function openAddWin() {
			mini.prompt("请输入数据库名称：", "请输入",
				function(action, value) {
					if(action == "ok") {
						var data = {
							"databaseName": value
						};
						tableList.addItem(data);
						tableList.select(tableList.getData().length - 1);
						setFieldGridData(data);
					}
				}
			);
		}

		function setFieldGridData(item) {
			var list = [{
				"key": "databaseName",
				"val": item.databaseName,
				"des": "数据库名"
			}, {
				"key": "description",
				"val": item.description,
				"des": "说明"
			}, {
				"key": "type",
				"val": item.type,
				"des": "数据库类型"
			}, {
				"key": "dataSourceClassName",
				"val": item.dataSourceClassName,
				"des": "连接池类名"
			}, {
				"key": "updateDatabase",
				"val": item.updateDatabase,
				"des": "是否更新数据库表结构"
			}, {
				"key": "updateDeleteField",
				"val": item.updateDeleteField,
				"des": "是否删除未定义的数据库字段"
			}, {
				"key": "defaultDatabase",
				"val": item.defaultDatabase,
				"des": "是否默认数据库"
			}, {
				"key": "driver",
				"val": item.driver,
				"des": "数据库驱动名称"
			}, {
				"key": "url",
				"val": item.url,
				"des": "URL"
			}, {
				"key": "username",
				"val": item.username,
				"des": "用户名"
			}, {
				"key": "password",
				"val": item.password,
				"des": "密码"
			}, {
				"key": "initialSize",
				"val": item.initialSize,
				"des": "初始化连接数"
			}, {
				"key": "minIdle",
				"val": item.minIdle,
				"des": "最小连接数"
			}, {
				"key": "maxActive",
				"val": item.maxActive,
				"des": "最大连接数"
			}, {
				"key": "maxWait",
				"val": item.maxWait,
				"des": "获取连接等待超时的时间"
			}, {
				"key": "timeBetweenEvictionRunsMillis",
				"val": item.timeBetweenEvictionRunsMillis,
				"des": "间隔多久才进行一次检测，检测需要关闭的空闲连接，单位是毫秒"
			}, {
				"key": "minEvictableIdleTimeMillis",
				"val": item.minEvictableIdleTimeMillis,
				"des": "一个连接在池中最小生存的时间，单位是毫秒"
			}, {
				"key": "validationQuery",
				"val": item.validationQuery,
				"des": "连接有效性检查语句"
			}, {
				"key": "testOnBorrow",
				"val": item.testOnBorrow,
				"des": "是否检查连接有效性"
			}, {
				"key": "testWhileIdle",
				"val": item.testWhileIdle,
				"des": "指明连接是否被空闲连接回收器(如果有)进行检验，如果检测失败，则连接将被从池中去除"
			}];
			fieldGrid.setData(list);
		}

		/*******************************************
		 * 保存一个单表表结构数据至内存中
		 *******************************************/
		function doSave() {
			mini.confirm("确定要保存当前选中的连接配置吗？", "确定？",
				function(action) {
					if(action == "ok") {

						var data = {};
						var list = fieldGrid.getData();
						for(var i = 0; i < list.length; i++) {
							var d = list[i];
							data[d.key] = d.val;
						}
						debugger
						http.post("/admin/database/save.do", data, function(page) {
							if(page.success) {
								mini.alert("保存成功", "提示", null);
							} else {
								mini.alert(page.message, "保存失败", null);
							}
						});
					}
				}
			);
		}

		/*******************************************
		 * 保存所有表结构数据至配置文件中
		 *******************************************/
		function doSaveAll() {
			// versionWin.hide();
			mini.confirm("确定要保存所有表结构吗？", "确定？",
				function(action) {
					if(action == "ok") {
						var data = {};
						debugger
						http.post("/admin/table/saveAll.do", data, function(page) {
							if(page.success) {
								mini.alert("保存成功", "提示", null);
							} else {
								mini.alert(page.message, "保存失败", null);
							}
						});
					}
				}
			);
		}

		/*******************************************
		 * 删除表
		 *******************************************/
		function doDelete() {
			if(tableList.getSelected()) {
				mini.confirm("确定要删除选中的表结构吗？", "确定？",
					function(action) {
						if(action == "ok") {
							var table = tableList.getSelected();
							var data = {
								"databaseName": table.databaseName,
								"tableName": table.tableName
							};
							http.post("/admin/table/deleteTable.do", data, function(page) {
								if(page.success) {
									mini.alert("数据库表结构删除成功", "提示", null);
									tableList.removeItem(tableList.getSelected());
									fieldGrid.setData([]);
								} else {
									mini.alert(page.message, "操作失败", null);
								}
							});
						}
					});
			} else {
				mini.alert("请先选择要删除的表！", "错误", null);
			}
		}
	</script>

</html>